package com.njbw.design_pattern.created.prototype.shallowCopy;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 浅拷贝——实现Cloneable接口
 * 播放列表
 *
 * @author 黄侯淳
 * @since JDK 1.8
 */
@Slf4j
@Data
public class PlayList2 implements Cloneable {

    /**
     * 播放列表ID
     */
    private Long id;

    /**
     * 播放列表名称
     */
    private String name;

    /**
     * 歌曲列表
     */
    private List<Song> songList;

    public PlayList2() {
        this.songList = new ArrayList<>();
    }

    public void addSong(Song song) {
        this.songList.add(song);
    }

    @Override
    public PlayList2 clone() throws CloneNotSupportedException {
        return (PlayList2) super.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        PlayList2 playList = new PlayList2();
        playList.setId(1L);
        playList.setName("周杰伦");
        playList.addSong(new Song("稻香", "周杰伦"));
        playList.addSong(new Song("七里香", "周杰伦"));
        playList.addSong(new Song("告白气球", "周杰伦"));

        // 浅拷贝后的最喜爱的专辑
        PlayList2 favouriteList = playList.clone();
        favouriteList.setName("最爱");
        favouriteList.addSong(new Song("曹操", "林俊杰"));
        log.info("favouriteList: {}", favouriteList);
    }
}
